using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using ShoppingCenter.Backend.Api.Db;
using ShoppingCenter.Backend.Api.Model;
using ShoppingCenter.Backend.Api.Params;
using ShoppingCenter.Backend.Api.Repository;
using ShoppingCenter.Backend.Api.Utils;

namespace ShoppingCenter.Backend.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]

    //规格控制器
    public class SpecificationsController : ControllerBase
    {
        private IConfiguration _configuration;

        //规格
        private IRepository<Specifications> _specificationsRepository;

        //属性
        private IRepository<Model.Attribute> _attributeRepository;
        //属性值
        private IRepository<Attribute_value> _attributeValueRepository;

        public SpecificationsController(IConfiguration configuration, IRepository<Model.Attribute> attributeRepository, IRepository<Attribute_value> attributeValueRepository, IRepository<Specifications> specificationsRepository)
        {
            _configuration = configuration;
            _specificationsRepository = specificationsRepository;
            _attributeRepository = attributeRepository;
            _attributeValueRepository = attributeValueRepository;
        }


        //获取所有规格
        public dynamic Get()
        {
            var currentPage = int.Parse(Request.Query["currentPage"][0]);
            var pageSize = int.Parse(Request.Query["pageSize"][0]);

            var shopingDb = new ShoppingCenterDb();

            // 分页
            var specifications = shopingDb.Specifications.Include(x => x.Attribute).Include(x => x.Attribute_value).ToList();
            var specification = specifications.Skip((currentPage - 1) * pageSize).Take(pageSize);

            return new
            {
                Code = 1000,
                Data = new
                {
                    Data = specification,
                    Pager = new { currentPage, pageSize, totalSize = specifications.Count() }
                },
                Msg = "获取规格表成功"
            };
        }


        //根据商品id获取指定规格
        [HttpGet("{merchandiseId}")]
        public dynamic Get(int merchandiseId)
        {
            var Attribute = _attributeRepository.Table.ToList();
            var attributeValue = _attributeValueRepository.Table.ToList();

            // 根据商品id获取指定规格
            var specifications = _specificationsRepository.Table.Where(x => x.MerchandiseId == merchandiseId);


            return new
            {
                Code = 1000,
                Data = specifications,
                Msg = "获取当前规格成功"
            };
        }

        //获取所有属性
        [HttpGet, Route("Attribute")]
        public dynamic GetAttribute()
        {

            var Attributes = _attributeRepository.Table.ToList();

            return new
            {
                Code = 1000,
                Data = Attributes,
                Msg = "获取所有属性成功"
            };
        }
        //获取所有属性值
        [HttpGet, Route("AttributeValue")]
        public dynamic GetAttributeValue()
        {

            var attributesValue = _attributeValueRepository.Table.ToList();

            return new
            {
                Code = 1000,
                Data = attributesValue,
                Msg = "获取所有属性值成功"
            };
        }




        //添加属性
        [HttpPost, Route("addAttribute")]
        public dynamic AddAttribute(CUR_Specifications newSpecifications)
        {
            var Attribute = _attributeRepository.Table.Where(x => x.Name == newSpecifications.Attribute).FirstOrDefault();

            if (Attribute == null)
            {
                var att = new Model.Attribute
                {
                    Name = newSpecifications.Attribute
                };

                _attributeRepository.Insert(att);

                return new
                {
                    Code = 1000,
                    Data = att,
                    Msg = "添加新属性成功"
                };
            }


            return new
            {
                Code = 1000,
                Data = Attribute,
                Msg = "获取属性成功"
            };
        }

        //添加属性值
        [HttpPost, Route("addAttributeValue")]
        public dynamic AddAttributeValue(CUR_Specifications newSpecifications)
        {
            var attributeValue = _attributeValueRepository.Table.Where(x => x.Name == newSpecifications.Attribute_value);

            if (attributeValue == null)
            {
                var attValue = new Attribute_value
                {
                    Name = newSpecifications.Attribute_value
                };

                _attributeValueRepository.Insert(attValue);

                return new
                {
                    Code = 1000,
                    Data = attValue,
                    Msg = "添加新属性值成功"
                };
            }


            return new
            {
                Code = 1000,
                Data = attributeValue,
                Msg = "获取属性值成功"
            };
        }

        //添加规格
        [HttpPost]
        public dynamic Post(CUR_Specifications newSpecifications)
        {
            //商品id不能为空，库存、价格（商品的每种规格都要重新定义价格）
            var merchandiseId = newSpecifications.MerchandiseId;


            if (merchandiseId <= 0)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "获取不到当前商品"
                };
            }

            var inventory = newSpecifications.Inventory;
            var price = newSpecifications.Price;

            if (inventory <= 0)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "当前商品规格的库存不能小于或等于0"
                };
            }


            // 判断获取到的属性和属性值存不存在
            var attribute = _attributeRepository.Table.Where(x => x.Name == newSpecifications.Attribute).FirstOrDefault();
            var attributeValue = _attributeValueRepository.Table.Where(x => x.Name == newSpecifications.Attribute_value).FirstOrDefault();

            //如果属性和属性值都为空
            if (attribute == null && attributeValue == null)
            {
                //将属性添加到对应的表中
                var att = new Model.Attribute
                {
                    Name = newSpecifications.Attribute
                };

                //将属性值添加到对应的表中
                var attValue = new Attribute_value
                {
                    Name = newSpecifications.Attribute_value
                };

                _attributeRepository.Insert(att);
                _attributeValueRepository.Insert(attValue);

                // 获取刚刚插入的属性id
                var attributeId = _attributeRepository.Table
                .Where(x => x.Name == newSpecifications.Attribute)
                .ToList()
                .LastOrDefault().Id;

                //获取刚刚插入的属性值id
                var attributeValueId = _attributeValueRepository.Table
                .Where(x => x.Name == newSpecifications.Attribute_value)
                .ToList()
                .LastOrDefault().Id;

                // 插入规格表
                var spec001 = new Specifications
                {
                    AttributeId = attributeId,
                    Attribute_valueId = attributeValueId,
                    MerchandiseId = merchandiseId,
                    Inventory = newSpecifications.Inventory,
                    Price = newSpecifications.Price
                };

                _specificationsRepository.Insert(spec001);

                return new
                {
                    Code = 1000,
                    Data = spec001,
                    Msg = "添加规格成功"
                };


            }

            if (attribute == null)
            {
                //将属性添加到对应的表中
                var att = new Model.Attribute
                {
                    Name = newSpecifications.Attribute
                };

                _attributeRepository.Insert(att);

                // 获取刚刚插入的属性id
                var attributeId = _attributeRepository.Table
                .Where(x => x.Name == newSpecifications.Attribute)
                .ToList()
                .LastOrDefault().Id;

                // 插入规格表
                var spec001 = new Specifications
                {
                    AttributeId = attributeId,
                    Attribute_valueId = attributeValue.Id,
                    MerchandiseId = merchandiseId,
                    Inventory = newSpecifications.Inventory,
                    Price = newSpecifications.Price
                };

                _specificationsRepository.Insert(spec001);

                return new
                {
                    Code = 1000,
                    Data = spec001,
                    Msg = "添加规格成功"
                };

            }

            if (attributeValue == null)
            {
                //将属性值添加到对应的表中
                var attValue = new Attribute_value
                {
                    Name = newSpecifications.Attribute_value
                };

                _attributeValueRepository.Insert(attValue);

                //获取刚刚插入的属性值id
                var attributeValueId = _attributeValueRepository.Table
                .Where(x => x.Name == newSpecifications.Attribute_value)
                .ToList()
                .LastOrDefault().Id;

                // 插入规格表
                var spec001 = new Specifications
                {
                    AttributeId = attribute.Id,
                    Attribute_valueId = attributeValueId,
                    MerchandiseId = merchandiseId,
                    Inventory = newSpecifications.Inventory,
                    Price = newSpecifications.Price
                };

                _specificationsRepository.Insert(spec001);

                return new
                {
                    Code = 1000,
                    Data = spec001,
                    Msg = "添加规格成功"
                };
            }

            //如果属性和属性值都不为空（表里都可以找到当前属性和值）

            // 插入规格表
            var spec = new Specifications
            {
                AttributeId = attribute.Id,
                Attribute_valueId = attributeValue.Id,
                MerchandiseId = merchandiseId,
                Inventory = inventory,
                Price = price
            };

            _specificationsRepository.Insert(spec);

            return new
            {
                Code = 1000,
                Data = spec,
                Msg = "添加商品规格成功"
            };

            // 将结果实例化后输出

        }


        //根据id修改规格信息
        [HttpPut("{id}")]
        public dynamic Put(int id, CUR_Specifications updateSpecifications)
        {
            //商品id不能为空，库存、价格（商品的每种规格都要重新定义价格）
            var merchandiseId = updateSpecifications.MerchandiseId;


            //   获取要修改的规格
            var specification = _specificationsRepository.GetById(id);

            if (merchandiseId <= 0)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "获取不到当前商品"
                };
            }

            // 获取库存和价格
            var inventory = updateSpecifications.Inventory;
            var price = updateSpecifications.Price;

            if (inventory <= 0)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "当前商品规格的库存不能小于或等于0"
                };
            }

            // 判断获取到的属性和属性值存不存在
            var attribute = _attributeRepository.Table.Where(x => x.Name == updateSpecifications.Attribute).FirstOrDefault();
            var attributeValue = _attributeValueRepository.Table.Where(x => x.Name == updateSpecifications.Attribute_value).FirstOrDefault();

            //如果属性和属性值都为空
            if (attribute == null && attributeValue == null)
            {
                //将属性添加到对应的表中
                var att = new Model.Attribute
                {
                    Name = updateSpecifications.Attribute
                };

                //将属性值添加到对应的表中
                var attValue = new Attribute_value
                {
                    Name = updateSpecifications.Attribute_value
                };

                _attributeRepository.Insert(att);
                _attributeValueRepository.Insert(attValue);

                // 获取刚刚插入的属性id
                var attributeId = _attributeRepository.Table
                .Where(x => x.Name == updateSpecifications.Attribute)
                .ToList()
                .LastOrDefault().Id;

                //获取刚刚插入的属性值id
                var attributeValueId = _attributeValueRepository.Table
                .Where(x => x.Name == updateSpecifications.Attribute_value)
                .ToList()
                .LastOrDefault().Id;


                specification.AttributeId = attributeId;
                specification.Attribute_valueId = attributeValueId;
                specification.MerchandiseId = merchandiseId;
                specification.Inventory = updateSpecifications.Inventory;
                specification.Price = updateSpecifications.Price;


                _specificationsRepository.Update(specification);

                return new
                {
                    Code = 1000,
                    Data = specification,
                    Msg = "修改规格成功"
                };


            }

            if (attribute == null)
            {
                //将属性添加到对应的表中
                var att = new Model.Attribute
                {
                    Name = updateSpecifications.Attribute
                };

                _attributeRepository.Insert(att);

                // 获取刚刚插入的属性id
                var attributeId = _attributeRepository.Table
                .Where(x => x.Name == updateSpecifications.Attribute)
                .ToList()
                .LastOrDefault().Id;


                specification.AttributeId = attributeId;
                specification.Attribute_valueId = attributeValue.Id;
                specification.MerchandiseId = merchandiseId;
                specification.Inventory = updateSpecifications.Inventory;
                specification.Price = updateSpecifications.Price;


                _specificationsRepository.Update(specification);

                return new
                {
                    Code = 1000,
                    Data = specification,
                    Msg = "修改规格成功"
                };

            }

            if (attributeValue == null)
            {
                //将属性值添加到对应的表中
                var attValue = new Attribute_value
                {
                    Name = updateSpecifications.Attribute_value
                };

                _attributeValueRepository.Insert(attValue);

                //获取刚刚插入的属性值id
                var attributeValueId = _attributeValueRepository.Table
                .Where(x => x.Name == updateSpecifications.Attribute_value)
                .ToList()
                .LastOrDefault().Id;

                specification.AttributeId = attribute.Id;
                specification.Attribute_valueId = attributeValueId;
                specification.MerchandiseId = merchandiseId;
                specification.Inventory = updateSpecifications.Inventory;
                specification.Price = updateSpecifications.Price;


                _specificationsRepository.Update(specification);

                return new
                {
                    Code = 1000,
                    Data = specification,
                    Msg = "修改规格成功"
                };
            }


            //如果属性和属性值都不为空（表里都可以找到当前属性和值）
            specification.AttributeId = attribute.Id;
            specification.Attribute_valueId = attributeValue.Id;
            specification.MerchandiseId = merchandiseId;
            specification.Inventory = updateSpecifications.Inventory;
            specification.Price = updateSpecifications.Price;

            _specificationsRepository.Update(specification);

            return new
            {
                Code = 1000,
                Data = specification,
                Msg = "修改规格成功"
            };

        }

        //根据id删除规格
        [HttpDelete("{id}")]
        public dynamic Dalete(int id)
        {

            var specification = _specificationsRepository.GetById(id);

            if (specification == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = string.Format("无法删除id为{0}的规格", id)
                };
            }

            _specificationsRepository.Delete(id);

            return new
            {
                Code = 1000,
                Data = "",
                Msg = "删除当前规格成功"
            };
        }


    }
}